/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.bean;

import gmail.SendMailOk;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import vehicle.bol.BrandBol;
import vehicle.bol.CarBOL;
import vehicle.bol.EmployeeBOL;
import vehicle.bol.OrdersBol;
import vehicle.entity.Brands;
import vehicle.entity.Employee;
import vehicle.entity.Orders;
import vehicle.entity.Vehicles;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.ItemSelectEvent;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author caodangtinh
 */
@ManagedBean
@SessionScoped
public class AdminVihicleBean {

    //setContentType("text/html;charset=UTF-8");
    /** Creates a new instance of AdminVihicleBean */
    List<Vehicles> listVihicle;
    CarBOL bOL = new CarBOL();
    List<Employee> listem;
    EmployeeBOL empBol = new EmployeeBOL();
    private String empName;
    private String empPassword;
    private Employee emp;

    public AdminVihicleBean() {
        createCategoryModel();
        createPieModel();
    }

    public List<Vehicles> getListVihicle() {
        if (emp.getPosition() == 1) {
            listVihicle = bOL.showAllVehicle();
        } else if (emp.getPosition() == 2) {
            listVihicle = bOL.showPosition(emp.getBrandID());
        } else {
            listVihicle = new ArrayList<Vehicles>();
        }
        return listVihicle;
    }

    public void setListVihicle(List<Vehicles> listVihicle) {
        this.listVihicle = listVihicle;
    }

    public String checkLogin() {
        try {
            emp = EmployeeBOL.checkLogin(empName, empPassword);
            // bolem.checkLogin(empName, empPassword);
            if (emp != null && emp.getPosition() == 1) {
                return "orderadmin.xhtml";
            } else if (emp.getPosition() == 2) {
                return "vehicleadmin.xhtml";
            } else {
                return "login.xhtml";
            }
        } catch (Exception e) {
            return "login.xhtml";
        }
        // EmployeeBOL bolem = new EmployeeBOL();

    }

    public String LogOut() {
        emp = null;
        return "login.xhtml";
    }

    public Employee getEmp() {
        return emp;
    }

    public void setEmp(Employee emp) {
        this.emp = emp;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public String getEmpPassword() {
        return empPassword;
    }

    public void setEmpPassword(String empPassword) {
        this.empPassword = empPassword;
    }
    //show order
    //list chua order
    private List<Orders> listOrder = new ArrayList<Orders>();
    OrdersBol bolOrder = new OrdersBol();
    //list tam luu tru listOrder de edit

    public List<Orders> getListOrder() {
        if (emp.getPosition() == 1) {
            if (!paymentandpailcheckbook) {
                if (searchdate) {
                    listOrder = bolOrder.searchorderbydateorder(1, formatDate(datetimeform), formatDate(datetimeto));
                } else {
                    listOrder = bolOrder.showOrders(1);
                }
            } else {
                if (searchdate) {
                    listOrder = bolOrder.searchorderbydateorder(0, formatDate(datetimeform), formatDate(datetimeto));
                } else {
                    listOrder = bolOrder.showOrders(0);
                }
                //searchdate = false;
            }
        } else {
            listOrder = new ArrayList<Orders>();
        }
        return listOrder;
    }
    ////////////////////////////////////////////////////////////////////
    private boolean paymentandpailcheckbook = false;

    public boolean isPaymentandpailcheckbook() {
        return paymentandpailcheckbook;
    }

    public void setPaymentandpailcheckbook(boolean paymentandpailcheckbook) {
        this.paymentandpailcheckbook = paymentandpailcheckbook;
    }

    public void paymentandpail() {
        if (paymentandpailcheckbook) {
            paymentandpailcheckbook = false;
        } else {
            paymentandpailcheckbook = true;
        }
    }

    public String paymentOrder() {
//        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
//        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
//        try {
//            request.setCharacterEncoding("UTF-8");
//        } catch (UnsupportedEncodingException ex) {
//            Logger.getLogger(AdminVihicleBean.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        //setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html;charset=UTF-8");
        //String to = "banglv_a04448@fpt.aptech.ac.vn";
        String to = ordpayment.getEmail();
        String subject = "Infometion Order";
        String body = "";
        body = body + "Your Name :" + ordpayment.getCusFullName() + "\n";
        body = body + "Address:" + ordpayment.getAddress() + "\n";
        body = body + "Email :" + ordpayment.getEmail() + "\n";
        body = body + "Phone :" + ordpayment.getPhoneNumber() + "\n";
        body = body + "Comment :" + ordpayment.getComments() + "\n";
        body = body + "Vehicle Name :" + vehipayment.getVehilceName() + "\n";
        body = body + "Price :" + ordpayment.getPrice() + "\n";
        body = body + "Employee Name :" + emp.getFullName() + "\n";
        body = body + "Date Regist :" + ordpayment.getDateRegis() + "\n";
        body = body + "Date Order :" + Calendar.getInstance().getTime() + "\n";
        body = body + "\nThanks your";
        int a = bolOrder.paymentOrder(ordpayment.getOrderID(), emp.getUserName());
        int send = SendMailOk.sendMail(to, subject, body);
        if (a == 1) {
            return "orderadmin.xhtml";
        } else if (send != 1) {
            return "failsSendmail.xhtml";
        } else {
            return "failsPayment.xhtml";
        }
    }
    //khoang
    private List<Vehicles> arrVehicle;

    public List<Vehicles> getArrVehicle() {
        if (emp.getPosition() == 1) {
            arrVehicle = bOL.showstatisticsVehicle();
        } else {
            arrVehicle = new ArrayList<Vehicles>();
        }

        return arrVehicle;
    }
    private List<Brands> listBrand = new ArrayList<Brands>();

    public List<Brands> getListBrand() {
        BrandBol bbol = new BrandBol();
        listBrand = bbol.ReportBrand();
        return listBrand;
    }
    private List<String> listbrandname = new ArrayList<String>();

    public List<String> getListbrandname() {
        BrandBol bbol = new BrandBol();
        listBrand = bbol.ReportBrand();
        for (Brands item : listBrand) {
            listbrandname.add(item.getBrandName());
        }
        return listbrandname;
    }
    //payment order
    private Orders ordpayment;
    private Vehicles vehipayment;

    public Orders getOrdpayment() {
        return ordpayment;
    }

    public void setOrdpayment(Orders ordpayment) {
        this.ordpayment = ordpayment;
    }

    public Vehicles getVehipayment() {
        return vehipayment;
    }

    public void setVehipayment(Vehicles vehipayment) {
        this.vehipayment = vehipayment;
    }

    public String updatePayment(Orders or) {
        ordpayment = or;
        vehipayment = CarBOL.searchVehicleByID(or.getVehicleID());
        if (or.getStatus() == 0) {
            return "paymentorderadmin.xhtml";
        } else {
            return "detailorderadmin.xhtml";
        }
    }
    //delete Order

    public void deleteOrder(Orders or2) {

        int a = bolOrder.deleteOrder(or2.getOrderID());

    }

    public void cancelOrder() {
    }
    //add order

    public String addOrder() {
        return "addorder.xhtml";
    }
    //update selectoneMenu
    private String checkupdate = "";

    public String getCheckupdate() {
        return checkupdate;
    }

    public void updateSelectOneMenu() {
        checkupdate = "ngon";
    }
    //search order date time
    private Date datetimeform;
    private Date datetimeto;

    public Date getDatetimeform() {
        return datetimeform;
    }

    public void setDatetimeform(Date datetimeform) {
        this.datetimeform = datetimeform;
    }

    public Date getDatetimeto() {
        return datetimeto;
    }

    public void setDatetimeto(Date datetimeto) {
        this.datetimeto = datetimeto;
    }
    boolean searchdate = false;

    public void searchOrderbydatetime() {
        if (datetimeform == null) {
            searchdate = false;
        } else if (datetimeto == null) {
            Calendar ca = Calendar.getInstance();
            ca.add(Calendar.DATE, 1);
            datetimeto = ca.getTime();
            searchdate = true;
        } else {
            searchdate = true;
        }
    }

    public void resetsearch() {
        datetimeform = null;
        datetimeto = null;
        searchdate = false;
    }
    //format datetime

    public String formatDate(Date time) {
        SimpleDateFormat form = null;
        if (time != null) {
            form = new SimpleDateFormat("yyyy-MM-dd");
        } else {
            return "";
        }
        return form.format(time);
    }
    ////////////////////////////////////////////////////////////////////////
    private CartesianChartModel categoryModel;
    private PieChartModel pieModel;

    public void itemSelect(ItemSelectEvent event) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Item selected",
                "Item Index: " + event.getItemIndex() + ", Series Index:" + event.getSeriesIndex());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public PieChartModel getPieModel() {
        return pieModel;
    }

    private void createCategoryModel() {
        categoryModel = new CartesianChartModel();

        ChartSeries boys = new ChartSeries();
        boys.setLabel("Boys");

        boys.set("2004", 120);
        boys.set("2005", 100);
        boys.set("2006", 44);
        boys.set("2007", 150);
        boys.set("2008", 25);

        ChartSeries girls = new ChartSeries();
        girls.setLabel("Girls");

        girls.set("2004", 52);
        girls.set("2005", 60);
        girls.set("2006", 110);
        girls.set("2007", 135);
        girls.set("2008", 120);

        categoryModel.addSeries(boys);
        categoryModel.addSeries(girls);
    }

    private void createPieModel() {
        pieModel = new PieChartModel();

        pieModel.set("Brand 1", 540);
        pieModel.set("Brand 2", 325);
        pieModel.set("Brand 3", 702);
        pieModel.set("Brand 4", 421);
    }
    ///////////////////////////////////////////////////////them sua xoa employee
    //////////////////add Employee
    private Employee empAdd = new Employee();

    public Employee getEmpAdd() {
        return empAdd;
    }

    public void setEmpAdd(Employee empAdd) {
        this.empAdd = empAdd;
    }

    public String addEmployee() {
        empAdd = new Employee();
        return "addEmployee.xhtml";
    }
    private String positionString = "Admin";

    public String getPositionString() {
        return positionString;
    }

    public void setPositionString(String positionString) {
        this.positionString = positionString;
    }
    private String brandNameAddEmp;

 

    public String getBrandNameAddEmp() {
        return brandNameAddEmp;
    }

    public void setBrandNameAddEmp(String brandNameAddEmp) {
        this.brandNameAddEmp = brandNameAddEmp;
    }

    public String addnewEmp() {


        if (positionString.equals("Admin")) {
            empAdd.setPosition(1);
        } else {
            empAdd.setPosition(2);
            if (brandNameAddEmp == null) {
                FacesMessage msg = new FacesMessage("Choice Brand ");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
        }
        Employee checkEmployee = EmployeeBOL.checkEmployee(empAdd.getUserName());
        if (checkEmployee != null) {
            FacesMessage msg = new FacesMessage("User Name =User Name ");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        } else {
            int a = 0;
            a = EmployeeBOL.insertEmployee(empAdd,brandNameAddEmp);
            empAdd = new Employee();
            if (a == 1) {
                return "EmployeeManage.xhtml";
            } else {
                FacesMessage msg = new FacesMessage("Error add Employee !!!");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
        }
        //return "EmployeeManage.xhtml";
    }
    //eidt Employee

    public String editEmployee(Employee em) {
        empAdd = em;
        return "editEmployee.xhtml";
    }

    public String editUpdateEmployee() {
        if (positionString.equals("Admin")) {
            empAdd.setPosition(1);
        } else {
            empAdd.setPosition(2);
        }
       //// empAdd.setBrandID(brandIDAddEmp);
        int a = 0;
        a = EmployeeBOL.eidtEmployee(empAdd,brandNameAddEmp);
        // empAdd = new Employee();

//        this.listem = empBol.showAllEmp();
        if (a == 1) {
            return "EmployeeManage.xhtml";
        } else {
            return null;
        }
    }

///////////////////////////////////////////////////////////////////////
    public List<Employee> getListem() {
        this.listem = empBol.showAllEmp();
        return listem;
    }

    public String deleteEmployee(String em) {
        int b = empBol.deleteEmployee(em);
//        this.listem = empBol.showAllEmp();
        return "EmployeeManage.xhtml";
    }
    ///////////////////////////////////////////////////////////////////////////////////////
    //edit price in vehicleAdmin
    private double pricenew = 10000.0;

    public double getPricenew() {
        return pricenew;
    }

    public void setPricenew(double pricenew) {
        this.pricenew = pricenew;
    }

    public String editPriceVehicle(Vehicles ve) {
        if (pricenew > 0 && pricenew < 9999999.0) {
            bOL.editPriceVehicle(ve.getVehicleID(), pricenew);
        }
        return null;
    }

    public String closeEditPrice() {
        return null;
    }
    ///////////////////////////////////////////////////////// change password
    private String oldpasswordchangepassword;
    private String newpasswordchangepassword;

    public String getNewpasswordchangepassword() {
        return newpasswordchangepassword;
    }

    public void setNewpasswordchangepassword(String newpasswordchangepassword) {
        this.newpasswordchangepassword = newpasswordchangepassword;
    }

    public String getOldpasswordchangepassword() {
        return oldpasswordchangepassword;
    }

    public void setOldpasswordchangepassword(String oldpasswordchangepassword) {
        this.oldpasswordchangepassword = oldpasswordchangepassword;
    }

    public String changePasswordchangepassword() {
        Employee checkacount = EmployeeBOL.checkLogin(emp.getUserName(), oldpasswordchangepassword);
        if (checkacount == null) {
            FacesMessage msg = new FacesMessage("Old Password fails !!!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } else {
            int checkChangepass = EmployeeBOL.changepassword(emp.getUserName(), newpasswordchangepassword);
            if (checkChangepass == 1) {
                FacesMessage msg = new FacesMessage("Change Password success !!!");
                FacesContext.getCurrentInstance().addMessage(null, msg);
            }
        }
        return null;
    }
}
